﻿using System;


namespace LoanCalc
{
    class Program
    {
        static void Main()
        {
            //ExecLoanCalc();
            ExecLoanCalcMain();
        }


        // Define other methods and classes here

        static string[,] LoanCalc(decimal P, int loanTerm, decimal interestRate,
                                                    DateTime startDate)
        {

            string[] results = new string[5];
            string[,] res = new string[loanTerm * 12, results.Length];
            decimal[] temp = new decimal[4];

            temp[0] = P;
            temp[2] = P / (loanTerm * 12);

            for (int i = 0; i < loanTerm * 12; i++)
            {
                temp[1] = temp[0] * interestRate / (12 * 100);
                temp[3] = temp[1] + temp[2];
                temp[0] -= temp[2];

                res[i, 0] = startDate.AddMonths(i).ToString("dd.MM.yyyy");
                res[i, 1] = temp[0].ToString("f2");
                res[i, 2] = temp[1].ToString("f2");
                res[i, 3] = temp[2].ToString("f2");
                res[i, 4] = temp[3].ToString("f2");
            }
            return res;
        } // array

        static void ExecLoanCalc()
        {
            DateTime startDate = DateTime.Now;

            //decimal P = 1000;
            Console.Write("Сумма для заема, грн:");
            decimal P = decimal.Parse(Console.ReadLine());
            //Console.WriteLine(" {0};", P);

            //int loanTerm = 1;
            Console.Write("Срок кредита, год:");
            int loanTerm = int.Parse(Console.ReadLine());
            //Console.WriteLine(" {0};", loanTerm);

            //decimal rate = 20;
            Console.Write("Процентная ставка годовых:");
            decimal rate = decimal.Parse(Console.ReadLine());
            //Console.WriteLine(" {0}%;", rate);
            Console.WriteLine("");
            string[] content = { "Дата", "Баланс", "Процент", "Осн. долг", "Сумма платежа" };

            for (int i = 0; i < content.Length; i++)
            {
                Console.Write("{0,15}", content[i]);
            }

            Console.WriteLine("");

            string[,] res = LoanCalc(P, loanTerm, rate, startDate);

            for (int i = 0; i < res.GetLength(0); i++)
            {
                for (int j = 0; j < res.GetLength(1); j++)
                {
                    Console.Write("{0,15}", res[i, j]);
                }
                Console.WriteLine("");
            }
            Console.ReadKey();
        } 

        static void ExecLoanCalcMain()
        {
            DateTime startDate = DateTime.Now;

            //decimal P = 1000;
            Console.Write("Сумма для заема, грн:");
            decimal P = decimal.Parse(Console.ReadLine());
            //Console.WriteLine(" {0};", P);

            //int loanTerm = 1;
            Console.Write("Срок кредита, год:");
            int loanTerm = int.Parse(Console.ReadLine());
            //Console.WriteLine(" {0};", loanTerm);

            //decimal rate = 20;
            Console.Write("Процентная ставка годовых:");
            decimal rate = decimal.Parse(Console.ReadLine());
            //Console.WriteLine(" {0}%;", rate);
            //Console.WriteLine("");
            Console.Write("Выберите тип кредита(1:Классический, 2:Аннуитетный):");
            

            LoanSchedule[] res;
            int choice = int.Parse(Console.ReadLine());
            if (choice == 1)
            {
                res = LoanCalcClassic(P, loanTerm, rate, startDate);
            }
            else
            {
                res = LoanCalcAnnuity(P, loanTerm, rate, startDate);
            }

            string[] content = { "Дата", "Задолженность", "Процент", "Осн. долг", "Сумма платежа" };

            Console.WriteLine("");
            for (int i = 0; i < content.Length; i++)
            {
                Console.Write("{0,15}", content[i]);
            }

            Console.WriteLine("");
            decimal[] total = new decimal[4];
            
            
            for (int i = 0; i < res.Length; i++)
            {
                    Console.Write("{0,15}", res[i].Date.ToString("dd.mm.yyyy"));
                    Console.Write("{0,15}", res[i].Debt.ToString("f2"));
                    Console.Write("{0,15}", res[i].Interest.ToString("f2"));
                    Console.Write("{0,15}", res[i].Pricipal.ToString("f2"));
                    Console.Write("{0,15}", res[i].MonthlyPayment.ToString("f2"));
                    Console.WriteLine("");
                    total[1] += res[i].Interest;
                    total[2] += res[i].Pricipal;
                    total[3] += res[i].MonthlyPayment;
                }
            total[0] = res[res.Length - 1].Debt - res[res.Length - 1].Pricipal;
            string tot = "Итого:";

            Console.WriteLine("");
            Console.WriteLine("{0,15}{1,15}{2,15}{3,15}{4,15}", tot, total[0].ToString("f2"), 
                total[1].ToString("f2"), total[2].ToString("f2"), total[3].ToString("f2"));
            Console.WriteLine("");
            Console.ReadKey();
        }

        static LoanSchedule[] LoanCalcClassic(decimal loanAmount, int loanTerm, 
                                                decimal interestRate, DateTime startDate)
        {
            LoanSchedule[] res = new LoanSchedule[loanTerm * 12];
            decimal temp = 0;
            for (int i = 0; i < loanTerm * 12; i++)
            {
                res[i] = new LoanSchedule();

                res[i].Date = startDate.AddMonths(i);
                res[i].Debt = loanAmount - temp;
                res[i].Pricipal = loanAmount / (loanTerm * 12);
                res[i].Interest = res[i].Debt * interestRate / (12 * 100);
                res[i].MonthlyPayment = res[i].Pricipal + res[i].Interest;
                temp += res[i].Pricipal;
            }
            return res;
        }

        static LoanSchedule[] LoanCalcAnnuity(decimal loanAmount, int loanTerm, 
                                                decimal interestRate, DateTime startDate)
        {

            LoanSchedule[] res = new LoanSchedule[loanTerm * 12];
            double j = (double)interestRate/ (12 * 100);
            double n = loanTerm * 12;
            double temp1 = (j * Math.Pow(1 + j, n)) / (Math.Pow(1 + j, n) - 1);
            decimal monthlyPayment = loanAmount * (decimal)temp1;
            decimal temp = 0;

            for (int i = 0; i < loanTerm * 12; i++)
            {
                res[i] = new LoanSchedule();

                res[i].Date = startDate.AddMonths(i);
                res[i].Debt = loanAmount - temp;        
                res[i].Interest = res[i].Debt * interestRate / (12 * 100);
                res[i].MonthlyPayment = monthlyPayment;
                res[i].Pricipal = monthlyPayment - res[i].Interest;
                temp += res[i].Pricipal;
            }
            return res;
        }
    }

    public class LoanSchedule
    {
        public DateTime Date;
        public decimal Interest; // проценты
        public decimal Pricipal; // основной долг
        public decimal Debt; // задолженность по кредиту
        public decimal MonthlyPayment; // сумма платежа
    }
   
}
